package com.ymtms.customerservice.controller;


import com.ymtms.customerservice.pojo.CallbackInformation;
import com.ymtms.customerservice.pojo.CollectingMoneyBalance;
import com.ymtms.customerservice.service.CollectionCallBackService;
import com.ymtms.customerservice.util.ValidateUtil;
import com.ymtms.customerservice.vo.GridRequest;
import com.ymtms.customerservice.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/collection-callback")
@Controller
public class CollectionCallBackController {
    @Autowired
    CollectionCallBackService collectionCallBackService;
    @RequestMapping("/newBillList")
    @ResponseBody
    public GridResult<CollectingMoneyBalance> listNewBill(
            GridRequest gridRequest,
            CollectingMoneyBalance collectingMoneyBalance,
            @RequestParam(value = "factDealDate", required = false)String factDealDate) {

        String startTime = "";
        String endTime = "";
        if (ValidateUtil.isNotEmpty(factDealDate)) {
            String[] split = factDealDate.split(" - ");
            startTime = split[0];
            endTime = split[1];
        }

        GridResult gridResult = collectionCallBackService.selectCollectionNewCallback(
                gridRequest, collectingMoneyBalance, startTime, endTime);

        // 清空指定的redis缓存
//          Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//           redisTemplate.delete(key);
        gridResult.setCode(0);
        gridResult.setMsg("");
        return gridResult;
    }

    /**
     * @description: 查看详情
     * @param gmid goodsbillmaster表主键
     * @return:  单个结果
     * @author: 邱斌雨
     * @date: 2019/11/5
     */
    @RequestMapping("/callbackDetail/{gmid}")
    @ResponseBody
    public Map callbackDetail(@PathVariable("gmid")Integer gmid){
        Map result = new HashMap();
        CollectingMoneyBalance collectingMoneyBalance = collectionCallBackService.queryByGmid(gmid);
        List<CollectingMoneyBalance> list = new ArrayList<>();
        list.add(collectingMoneyBalance);
        result.put("data",list);
        result.put("count",1);
        result.put("code",0);
        result.put("msg","");
        return result;
    }



    @RequestMapping("/executeCallback/{cmsid}")
    @ResponseBody
    public Map executeCallback(@PathVariable("cmsid")Integer cmsid){
        Map result = new HashMap();
        /*try {*/
        collectionCallBackService.executeCallback(cmsid);
        /*}catch(Exception e){
            result.put("code",500);
            result.put("msg",e.getMessage());
            return result;
        }*/
        result.put("code",0);
        result.put("msg","回告成功");
        return result;
    }

    @RequestMapping("/historyList")
    @ResponseBody
    public GridResult<CallbackInformation> listNewBill(
            GridRequest gridRequest,
            CallbackInformation CallbackInformation,
            @RequestParam(value = "finallyDialTime", required = false)String finallyDialTime) {

        String startTime = "";
        String endTime = "";
        if (ValidateUtil.isNotEmpty(finallyDialTime)) {
            String[] split = finallyDialTime.split(" - ");
            startTime = split[0];
            endTime = split[1];
        }

        GridResult gridResult = collectionCallBackService.selectCallbackHistoryByPage(
                gridRequest, CallbackInformation, startTime, endTime);

        // 清空指定的redis缓存
//          Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//           redisTemplate.delete(key);
        gridResult.setCode(0);
        gridResult.setMsg("");
        return gridResult;
    }
    @RequestMapping("/removeHistory/{cbiid}")
    @ResponseBody
    public Map removeHistory(@PathVariable("cbiid")Integer cbiid){
        Map result = new HashMap();
        try {
            collectionCallBackService.removeHistory(cbiid); // 删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
            return result;
        }
        return result;
    }


}
